Place為一abstract object type,只有一個必填的name property。
abstract type Place {
required name: str {
delegated constraint exclusive;
};
}
由於delegated與overloaded是兩個初學者常常搞混的概念,在此一併說明。
delegated是想將constraint的責任由本身放寬至後續所繼承的object type時使用。
考慮schema如下:
abstract type Place {
required name: str {
delegated constraint exclusive;
};
}
type Landmark extending Place;
type Location extending Place;
Place object type的name property使用delegated將constraint exclusive放寬至後續extending它的Landmark object type及Location object type身上。
舉例來說,假設name property為「"艾菲爾鐵塔"」,在沒有使用delegated的情況下,我們只能在Landmark及Location兩個object type中,選擇一個來生成。換句話說,我們只能有name property為「"艾菲爾鐵塔"」的Landmark object或是name property為「"艾菲爾鐵塔"」的Location object,無法兩者並存。
但在使用delegated的情形下,我們可以同時擁有一個name property為「"艾菲爾鐵塔"」的Landmark object及一個name property為「"艾菲爾鐵塔"」的Location object。
select (insert Landmark {name:="艾菲爾鐵塔"}) {name};
{default::Landmark {name: '艾菲爾鐵塔'}}
select (insert Location {name:="艾菲爾鐵塔"}) {name};
{default::Location {name: '艾菲爾鐵塔'}}
overloaded是針對所繼承的property和link,想加入更嚴格的限制條件時使用。
考慮schema如下:
abstract type Person {
name : str
}
type PersonWithLongName extending Person {
overloaded name : str {
constraint min_len_value(10)
}
}
其中name property在Person中僅需要為str型態,而在PersonWithLongName中,我們使用overloaded更進一步限制其不只是要為str型態且長度不能小於10。
此時如果insert一個name property「"John"」的PersonWithLongName object:
insert PersonWithLongName {name:="John"};
會報錯如下:
edgedb error: ConstraintViolationError: name must be no shorter than 10 characters.
Detail: violated constraint 'std::min_len_value' on property 'name' of object type 'default::PersonWithLongName'
如果insert一個name property為「"Christopher"」的PersonWithLongName object:
select (insert PersonWithLongName {name:="Christopher"}) {name};
則可以順利生成:
{default::PersonWithLongName {name: 'Christopher'}}
Landmark用來代表知名度較高的地標。
type Landmark extending Place;
Location用來代表一般地點。
type Location extending Place;
Store用來代表店鋪。
type Store extending Place;